#include <iostream>
#include <vector>
#include <algorithm>

class Student
{
private:
    int age;
    std::string name;

public:
    Student(int age, std::string name);
    ~Student() = default;
    std::string getName() const;
    int getAge() const;

    bool operator==(const Student &stu)
    {
        return stu.age == this->age && this->name == stu.name;
    }
};

Student::Student(int age, std::string name) : name(name), age(age)
{
}

std::string Student::getName() const
{
    return name;
}

int Student::getAge() const
{
    return age;
}

struct Print
{
    void operator()(const Student &stu)
    {
        std::cout << "name = " << stu.getName() << ", age = " << stu.getAge() << std::endl;
    }
};

void test_find01()
{
    std::vector<int> v{};
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i + 100);
    }

    std::vector<int>::const_iterator it = std::find(v.begin(), v.end(), 100);
    if (it != v.end())
    {
        std::cout << "找到了元素 " << *it << std::endl;
    }
    else
    {
        std::cout << "没有找到元素" << std::endl;
    }
}

void test_find02()
{
    std::vector<Student> stuVector{};
    for (int i = 0; i < 10; i++)
    {
        stuVector.push_back(Student(i + 18, std::string("Student") + std::to_string(i)));
    }

    // 使用 std::find 查找学生
    std::vector<Student>::const_iterator it = std::find(stuVector.begin(), stuVector.end(), Student(20, "Student2"));

    if (it != stuVector.end())
    {
        std::cout << "找到了学生：" << std::endl;
        Print()(stuVector[it - stuVector.begin()]); // 调用 Print 结构体的 operator()
    }
    else
    {
        std::cout << "没有找到学生" << std::endl;
    }
}

int main(int argc, char const *argv[])
{
    test_find01();
    test_find02();
    return 0;
}
